<?php

/**
 * Department models
 *
 * Department models in data
 *
 * PHP Version: 1.2.3
 *
 * @author Johari <joharijumali@gmail.com>
 */

/**
 * Class Department
 *
 * Class Department in data models
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Data_Department_Department extends Eloquent
{

    public static $timestamps = true;
    public static $table = 'sys_user_jabatan';
    public static $key = 'suj_no_auto';
    public static $rules = array(
                                'suj_jabatan_name' => 'required|Unique:wt_par_jabatan',
                            );

    /**
     * Function validate that pass parameter
     *
     * @param varchar $data the varchar to quote
	 *
	 * @return parameter.
     */
    public static function validate($data)
	{
        return Validator::make($data, Static::$rules);
    }
		
	/**
     * Function listDepartment
	 *
	 * @return listDepartment.
     */
    public static function listDepartment()
	{
        $allData = Data_Department_Department::order_by('suj_jabatan_name','ASC')
			->paginate(Config::get('system.pagination'));

        $datagrid = new Datagrid;
        $datagrid->setFields(array('suj_jabatan_name' => Str::upper(Lang::line('admin.department')->get())));
		$datagrid->setFields(array('suj_jabatan_desc' => Str::upper(Lang::line('global.desc')->get())));
        $datagrid->setAction(Lang::line('admin.division')->get(), 'division', false);
        $datagrid->setAction(Lang::line('admin.unit')->get(), 'unit', false);
        $datagrid->setAction(Lang::line('global.edit')->get(), 'editDept', true, array('suj_no_auto'));
        $datagrid->setAction(Lang::line('global.delete')->get(), 'deleteDept', true, array('suj_no_auto', 'suj_jabatan_name'));
        $datagrid->setTable('wt_par_jabatan', 'table table-bordered table-hover table-striped table-condensed');
        $datagrid->showNumbering(true);
        $datagrid->build($allData, static::$key);

        return $datagrid->render();
    }
	
	/**
     * Function addDepartment that pass parameter
     *
     * @param varchar $input the varchar to quote
	 *
	 * @return parameter.
     */
    public static function addDepartment($input)
	{
        if ($input['wpj_no_auto'] == null) {
            $department = new Data_Department_Department;
            $action = Lang::line('global.add')->get();
            $department->suj_cipta_oleh = Auth::user()->su_id;
        } else {
            $department = Data_Department_Department::find($input['wpj_no_auto']);
            $action = Lang::line('global.edit')->get();
        }
        
        $department->suj_jabatan_name = Str::title($input['wpj_jabatan_name']);
        $department->suj_jabatan_desc = $input['wpj_jabatan_desc'];
        $department->suj_kemaskini_oleh = Auth::user()->su_id;
        $department->save();

        Log::write(
			Lang::line('admin.department')->get(), 
			$action.' <strong>'.$input['wpj_jabatan_name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.department')->get(), 
			Auth::user()->su_id, 
			$action.' <strong>'.$input['wpj_jabatan_name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    }
	
	public static function editDepartment($input)
	{
		$department = Data_Department_Department::find($input['wpj_no_auto']);
		$action = Lang::line('global.edit')->get();
        
        $department->suj_jabatan_name = Str::title($input['wpj_jabatan_name']);
        $department->suj_jabatan_desc = $input['wpj_jabatan_desc'];
        $department->suj_kemaskini_oleh = Auth::user()->su_id;
        $department->save();

        Log::write(
			Lang::line('admin.department')->get(), 
			$action.' <strong>'.$input['wpj_jabatan_name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.department')->get(), 
			Auth::user()->su_id, 
			$action.' <strong>'.$input['wpj_jabatan_name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    }

	/**
     * Function deptInfo that pass parameter
     *
     * @param integer $id the integer to quote
	 *
	 * @return parameter.
     */
    public static function deptInfo($id)
	{
		$data = array();
		$raw = Data_Department_Department::find($id);
		$data['wpj_no_auto'] = $raw->suj_no_auto;
		$data['wpj_jabatan_name'] = $raw->suj_jabatan_name;
		$data['wpj_jabatan_desc'] = $raw->suj_jabatan_desc;

		return $data;
    }

	/**
     * Function deleteDept that pass parameter
     *
     * @param varchar $input the varchar to quote
	 *
	 * @return parameter.
     */
    public static function deleteDept($input)
	{
        Data_Department_Department::find($input['id'])->delete();

        Log::write(
			Lang::line('admin.department')->get(), 
			Lang::line('global.delete')->get().' <strong>'.$input['name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
		Trail::log(
			Lang::line('admin.department')->get(), 
			Auth::user()->su_id, 
			Lang::line('global.delete')->get().' <strong>'.$input['name'].'</strong> 
			'.Lang::line('global.by')->get().' <code>'.Auth::user()->su_username.'</code>'
		);
    } 

	/**
     * Custom Data Validation
     *
     * @param integer $id the integer to quote
	 *
	 * @return void.
     */
    public static function validateData($id)
	{
        $bil = Console_Profile::where('suj_no_auto', '=', $id)->count();
        $bil = $bil + Data_Department_Division::where('suj_no_auto', '=', $id)->count();
        $bil = $bil + Data_Department_Unit::where('suj_no_auto', '=', $id)->count();

        if ($bil > 0) {
            return false;
        } else {
            return true;
        }
    }
	
	/**
     * Function arrayDept
	 *
	 * @return arrayDept.
     */
    public static function arrayDept()
    {
        $deptlist = Data_Department_Department::order_by('suj_no_auto', 'asc')->get();

        $arrayDept = array('Sila Pilih');

        foreach ($deptlist as $value) {
            $arrayDept[$value->suj_no_auto] = $value->suj_jabatan_name;
        }

        return $arrayDept;
    }

}

?>